%% test it yoursel - multi partion
close all
mypic = imread('./demo_pics/phase_ice.pgm'); % pick your picture hear
subplot(2,2,1) 
imshow(mypic)
title('Original image')

res = multi_partition(edge(mypic),0.7,[0.0,0.1,0.1,0.1,0.0],3);
subplot(2,2,2) 
imshow(res)
title('Regular')

%% Demo - best partition vs. frequency partition
% example of low frequncy on real image
close all
mypic = imread('./demo_pics/phase_ice.pgm');
subplot(2,2,1) 
imshow(mypic)
title('Original image')

res = partition(edge(mypic),[0.0,0.1,0.1,0.1,0.0]);
subplot(2,2,2) 
imshow(res)
title('Regular')

[res2,~] = partition(edge(mypic),[0.0,0.0,0.0,0.0,0.0],20);
subplot(2,2,3) 
imshow(res2)
title('Medum frequency')

[res3,~] = partition(edge(mypic),[0.0,0.0,0.0,0.0,0.0], 300);
subplot(2,2,4) 
imshow(res3)
title('Very low frequency')

figure
mypic = imread('./demo_pics/watter_trees.pgm');
subplot(2,2,1) 
imshow(mypic)
title('Original image')

res = partition(edge(mypic,'canny'),[0.0,0.1,0.1,0.1,0.0]);
subplot(2,2,2) 
imshow(res)
title('Regular')

[res2,s2] = partition(edge(mypic,'canny'),[0.0,0.0,0.0,0.0,0.0], 5);
subplot(2,2,3) 
imshow(res2)
title('Medum frequency')

[res3,s3] = partition(edge(mypic,'canny'),[0.0,0.0,0.0,0.0,0.0], 300);
subplot(2,2,4) 
imshow(res3)
title('Very low frequency')

figure
mypic = imread('./demo_pics/phase_lake.pgm');
map = edge(mypic);
subplot(2,2,1) 
imshow(mypic)
title('Original image')

res = partition(map,[0.0,0.1,0.5,0.1,0.0]);
subplot(2,2,2) 
imshow(res)
title('Horizontal Partion')

[res2,s2] = partition(map,[0.0,0.0,0.0,0.0,0.0], 5);
subplot(2,2,3) 
imshow(res2)
title('Medum frequency')

[res3,s3] = partition(map,[0.0,0.0,0.0,0.0,0.0], 100);
subplot(2,2,4) 
imshow(res3)
title('Low frequency')
%% syntetic frequency test
figure
mypic = imread('./demo_pics/syntetic_script.pgm');
map = edge(mypic);
subplot(3,2,1) 
imshow(mypic)
title('Original image')

res = partition(map,[0.0,0.1,0.5,0.1,0.0]);
subplot(3,2,2) 
imshow(res)
title('Horizontal Partion')

[res2,s2] = partition(map,[0.0,0.0,0.0,0.0,0.0],50);
subplot(3,2,3) 
imshow(res2)
title('Medum frequency')

[res3,s3] = partition(map,[0.0,0.0,0.0,0.0,0.0],10);
subplot(3,2,4) 
imshow(res3)
title('high frequency')

[res4,s4] = partition(map,[0.0,0.0,0.0,0.0,0.0], 100);
subplot(3,2,5) 
imshow(res4)
title('Low frequency')


%% Demo - multi partition
close all
demo_pics = {'./demo_pics/phase_ice.pgm';
    './demo_pics/watter_mountain.pgm';
    './demo_pics/snowy_peak.pgm';};
for i = 1:length(demo_pics)
    mypic = imread(demo_pics{i});
    subplot(2,length(demo_pics),i) 
    imshow(mypic)
    title('Original image')

    res = multi_partition(edge(mypic),0.7,[0.0,0.1,0.1,0.1,0.0],3);
    subplot(2,length(demo_pics),3 + i) 
    imshow(res)
    title('Recursive partition')
end

%% Demo -  multi partition edge vs. gradient
close all
demo_pics = {'./demo_pics/gradient_reflection.pgm';};

for i = 1:length(demo_pics)
    mypic = imread(demo_pics{i});
    subplot(length(demo_pics),3,3*i-1) 
    imshow(mypic)
    title('Original image')

    res = multi_partition(edge(mypic),0.7,[0.0,0.1,0.1,0.1,0.0],3);
    subplot(length(demo_pics),3,3*i-2) 
    imshow(res)
    title('Multi partition using edge')
    
    [Px,Py] = gradient(double(mypic));
    t = 15;
    x = (Px.^2+Py.^2).^0.5;
    map = ((x>t).*x)/max(max(x));

    res2 = multi_partition(map,0.2,[0.0,0.1,0.1,0.1,0.0],3);
    subplot(length(demo_pics),3,3*i) 
    imshow(res2)
    title('Multi partition using gradient')
end
%%  Demo -  multi partition fails

figure
mypic = imread('./demo_pics/phase_rocks_fail.pgm');
map = edge(mypic);
subplot(2,2,1) 
imshow(mypic)
title('Original image')

subplot(2,2,2) 
edge(mypic)
title('Sobel Edge-map')

[res2,s2] = partition(map,[0.0,0.0,0.0,0.0,0.0], 5);
subplot(2,2,3) 
imshow(res2)
title('High frequency')

[res3,s3] = partition(map,[0.0,0.0,0.0,0.0,0.0], 300);
subplot(2,2,4) 
imshow(res3)
title('Low frequency')

%% tierd labeling frequency partition example (including compilation)
addpath(genpath('./tiered_labeling'));

mex -setup % this is nessary only on first use
build % this is nessary only on first use and when changing source
files = getpictureset('demo_pics','freq_test'); % first arg is the directory  second isthe name of the file set
tiered_params = {[0.3,0],[3,0]};

figure;
n = length(files);
m = length(tiered_params);
total = m*n;

for i = 1:n %loop the files
    in = imread(files{i},'pgm');
    subplot(n,m+1,(m+1)*i), imshow(in);
    in = double(in);
    for j = 1:m
        out = uint8(maintieredmat(in, tiered_params{j}(1))); %this is the old c version
        subplot(n,m+1,(m+1)*i-j), imshow(out);
        disp(['[ ' , int2str((i-1)*m + j), ' / ',int2str(total),' ]'] );
    end
end
